package com.线程案例.compleable;

import java.util.concurrent.*;

/**
 * @author: LCG
 * @date: 2022-08-06 19:56:06
 * @description:
 **/
public class YYYYY {

    private static ThreadPoolExecutor threadPoolExecutor=
            new ThreadPoolExecutor(1,2,60, TimeUnit.SECONDS,new ArrayBlockingQueue<>(4));

    private static ThreadPoolExecutor threadPoolExecutor2=
            new ThreadPoolExecutor(1,2,60, TimeUnit.SECONDS,new ArrayBlockingQueue<>(4));

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<String> stringCompletableFuture = CompletableFuture.supplyAsync(() -> {
            System.out.println("==============执行业务");
            try {
                /**在这里在进行一个异步调用的程，使用同一个线程池**/
                YYYYY.tttt();
                //用休眠模拟业务的执行
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return "888880";
        }, threadPoolExecutor);

        String s = stringCompletableFuture.get();
    }


    public static void tttt() throws ExecutionException, InterruptedException {
        CompletableFuture<String> t1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("======执行t1=====");
            try {
                //用休眠模拟业务的执行
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "5555";
        }, threadPoolExecutor2);

        CompletableFuture<String> t2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("======执行t2=====");
            try {
                //用休眠模拟业务的执行
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "3333";
        }, threadPoolExecutor2);

        CompletableFuture<String> t3 = CompletableFuture.supplyAsync(() -> {
            System.out.println("======执行t3=====");
            try {
                //用休眠模拟业务的执行
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "3333";
        }, threadPoolExecutor2);

        CompletableFuture.allOf(t1, t2,t3).join();
        t1.get();
        t2.get();
        t3.get();
        System.out.println("=======执行完毕======");

    }


}
